from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from preceda_global import *
from preceda_messages import *
from preceda_genfunc import *
from preceda_fields import *
import unittest, time, re, csv, logging, datetime

class mainmenu:
    def __init__(self,objmenu):
        # =========================== Start: Menu ===========================
        # Created by: Giovanni Minoza
        # Date Created: August 12, 2013
        # Purpose: Menu
        # Country: AU
        # =========================== Start: Menu ===========================
        self.breakmenu(2,objmenu)
        globalflag.chb2v.implicitly_wait(30)
        # =========================== End: Menu ===========================

    def breakmenu(self,gsec,gtxt):
        # =========================== Start: Duplicate Submenu ===========================
        # Created by: Giovanni Minoza
        # Date Created: June 10, 2013
        # Date Modified: August 14, 2013
        # Purpose: For duplicates submenus within the same menu
        # self=driver, gsec=seconds, gtxt=element txt
        # =========================== Start: Duplicate Submenu ===========================
        driver=globalflag.chb2v
        menulist=gtxt.split(';')
        i=0
        g=0
        tmp=False
        tmpg=""
        a=datetime.datetime.now().time()
        a1=datetime.datetime.now()
        # print "Menu Start Time: " + str(a.strftime("%H:%M:%S"))
        for x in menulist:
            g=int(g)+1
            driver.implicitly_wait(10)
            # print str(g) + "-" + str(x)
            if int(g)==1:
                for i in range(gsec):
                    try:
                        driver.find_element_by_xpath("//label[contains(text(), \""+x+"\")]").click()
                        globalflag.tsdr=globalflag.tsdr+x+"/"
                        tmp=True
                        break
                    except:
                        time.sleep(1)
                        tmp=False
            elif int(g)==2:
                for i in range(gsec):
                    try:
                        driver.find_element_by_xpath("//div[contains(@class, \"x-grid-cell-inner\") and text()=\""+x+"\"]").click()
                        globalflag.tsdr=globalflag.tsdr+x+"/"
                        tmp=True
                        break
                    except:
                        time.sleep(1)
                        tmp=False
            else:
                for i in range(gsec):
                    try:
                        driver.find_element_by_xpath("//td[contains(@class, \"x-nlg-n-nav-item\")]/div[contains(@class, \"x-grid-cell-inner\") and text()=\""+x+"\"]").click()
                        globalflag.tsdr=globalflag.tsdr+x+"/"
                        tmp=True
                        break
                    except:
                        time.sleep(1)
                        tmp=False

        globalflag.flagtocontinue=tmp
        b=datetime.datetime.now().time()
        b1=datetime.datetime.now()
        c=b1-a1
        
        if tmp==True:
            # tmp1="Screen-Start: " + str(a.strftime("%H:%M:%S")) + " / " + "Screen-End: " + str(b.strftime("%H:%M:%S"))
            print "Time it took to process the following:"
            tmp2="  Menu and Screen: " + str(c)
            #print tmp1
            print tmp2
            #writelogs(self,globalini.joblog,globalini.csvpath,tmp1)
            #writelogs(self,globalini.joblog,globalini.csvpath,tmp2)
            #c=b-a
            #datetime.timedelta(0, 8, 562000)
            #print str(divmod(c.days * 86400 + c.seconds, 60))
            
        driver.implicitly_wait(15)
        # =========================== End: Duplicate Submenu ===========================
